clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

*SET CONTROL VARIABLES
local firm_controls "log_salesg_real_1lag_std size_assets_1lag_std liquidity_1lag_std pcmq_1lag_std recpay2yq_1lag_std d2aq_1lag_std ln_market_cap_q_1lag_std tobins_q_1lag_std lev_d2c_1lag_std dd_1lag_std age_1lag_std i.sector i.fqtr"

local agg_controls "lngdp inflation unemp nasdaq fedfunds"

*************
***Options***
*************
*Set ivol measure to use as baseline
local matur m1545
*Set mp shock to use as baseline
local mps ff4_d

* Set the LP details ========================================================================
*# periods to compute the responses foreach
local hor = 20
*# lags of "endogenous" variables 
local lagy = 4 
*# lags of "shock"
local lage = 0 

********************************************************************************
********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}Compustat_Quarterly_Final", clear

local n = 20

*Set panel data
xtset firmid ts_qtr

*Flag sample for those in at least n quarters
bysort gvkey pre_crisis: egen obs_num_temp = count(gvkey) if pre_crisis
bysort gvkey: egen obs_num = max(obs_num_temp)

*Flag outliers (bottom/top 0.5%) for investment
_pctile investment_intensive if pre_crisis, p(.5 99.5)
gen 	investment_outlier = (investment_intensive<r(r1)|investment_intensive>r(r2))

gen sample_period=(pre_crisis==1)

gen in_sample = (obs_num>=`n' & !missing(obs_num) & investment_outlier==0 & sample_period)

********************************************************************************
*Set panel data
xtset firmid ts_qtr

gen ivol_`matur'_std = ivol_`matur'
gen vix_eoq_std = vix_eoq
gen dd_std = dd
gen age_1lag_std = age

rename age age_1lag

***Standardize quarterly Compustat variables and lagged ivol level
foreach i in dd_1lag age_1lag log_salesg_real_1lag size_assets_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2c_1lag ivol_`matur' vix_eoq {

	*Pre-Crisis Standardization
	drop `i'_std
	qui: sum `i' if in_sample
	gen `i'_std = (`i' - r(mean)) / r(sd)
	
}

*Standardize ivol and dd
_pctile ivol_`matur' if in_sample, percentiles(30 70)
gen ivol_`matur'_hi2lo = (ivol_`matur' - r(r1)) / (r(r2) -  r(r1))

_pctile dd  if in_sample, percentiles(30 70)
gen dd_hi2lo = (dd - r(r1)) / (r(r2) -  r(r1))

*Standardize mps to unit standard deviation & positive is expansionary
gen `mps'=`mps'_shock_qtr
egen date_tag=tag(ts_qtr)

sum `mps' if pre_crisis & date_tag
replace `mps' = `mps'/r(sd)

gen `mps'_zero = 0
gen `mps'_exp = 0
gen `mps'_con = 0

replace `mps'_zero = `mps' if `mps'==0
replace `mps'_exp = `mps' if `mps'>0 & !missing(`mps')
replace `mps'_con = `mps' if `mps'<0
********************************************************************************
*Prepare local projection variables
********************************************************************************
xtset firmid ts_qtr

*Use the inflation-adjusted versions of dependent variables, if applicable
foreach i in niq ivltq ivstq invtq intanq tanq xrdq saleq K_int K_int_Know K_int_Org K_int_offBS K_int_onBS {
	
	drop `i'
	rename `i'_real `i'
 
}


foreach var in capital_stock K_int_Know_qtr K_int_Org_qtr ppentq {
*	foreach var in capital_stock niq ivltq ivstq invtq current_total_orig liquidity intanq tanq K_int K_int_Know K_int_Org K_int_offBS K_int_onBS K_tot xrdq saleq tobins_q_wins q_tot_wins K_phys_qtr K_phys_qtr_proxy K_int_qtr K_int_onBS_qtr K_int_offBS_qtr K_int_Know_qtr K_int_Org_qtr K_tot_qtr {
	forvalues h =   0/`hor' {
		 gen `var'`h' = (ln(f`h'.`var') - ln(l.`var')) * 100
}
}

*Create time variable for DK SEs
xtset
egen time=group(ts_qtr)
xtset firmid time

drop if !in_sample

	foreach var in capital_stock { 
	
	eststo clear
		
	cap gen qtrs = _n-1 if _n<=`hor'+1
	cap gen zero =  0 	if _n<=`hor'+1
	
	cap gen b_mps_exp = 0
	cap gen u_mps_exp = 0
	cap gen d_mps_exp = 0
	cap gen uu_mps_exp = 0
	cap gen dd_mps_exp = 0
	
	cap gen b_mps_con = 0
	cap gen u_mps_con = 0
	cap gen uu_mps_con = 0
	cap gen d_mps_con = 0
	cap gen dd_mps_con = 0

	cap gen b_intEff = 0
	cap gen u_intEff = 0
	cap gen d_intEff = 0
	cap gen uu_intEff = 0
	cap gen dd_intEff = 0
	
	cap gen b_mpsE = 0
	cap gen u_mpsE = 0
	cap gen d_mpsE = 0
	cap gen uu_mpsE = 0
	cap gen dd_mpsE = 0

	cap gen b_intEffE = 0
	cap gen u_intEffE = 0
	cap gen d_intEffE = 0
	cap gen uu_intEffE = 0
	cap gen dd_intEffE = 0
	
	cap gen b_mpsC = 0
	cap gen u_mpsC = 0
	cap gen d_mpsC = 0
	cap gen uu_mpsC = 0
	cap gen dd_mpsC = 0

	cap gen b_intEffC = 0
	cap gen u_intEffC = 0
	cap gen d_intEffC = 0
	cap gen uu_intEffC = 0
	cap gen dd_intEffC = 0

	cap gen nobs = 0
	
	********************************************************************************
	*INVESTMENT REGRESSIONS
	********************************************************************************

	*1. Investment on mps (plus interaction with ivol level)
	forvalues h = 0/`hor' {

		reghdfe `var'`h' `mps'_exp `mps'_zero `mps'_con L.ivol_`matur'_hi2lo  c.`mps'_exp#c.L.ivol_`matur'_hi2lo c.`mps'_zero#c.L.ivol_`matur'_hi2lo c.`mps'_con#c.L.ivol_`matur'_hi2lo  `firm_controls' l(1/`lagy').(`agg_controls') if in_sample, a(firmid) vce(cluster time,dkraay(16)) old
		
		lincom _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo] - _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]
		replace b_intEff = r(estimate)                  if _n == `h'+1
		replace u_intEff = r(estimate) + 1.645* r(se)  	if _n == `h'+1
		replace d_intEff = r(estimate) - 1.645* r(se) 	if _n == `h'+1	
		replace uu_intEff = r(estimate) + r(se)  	if _n == `h'+1
		replace dd_intEff = r(estimate) - r(se) 	if _n == `h'+1	
		
		replace b_mps_exp = _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]                     	if _n == `h'+1
		replace u_mps_exp = _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo] + 1.645* _se[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace d_mps_exp = _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo] - 1.645* _se[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace uu_mps_exp = _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo] + _se[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace dd_mps_exp = _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo] - _se[c.`mps'_exp#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1	
		
		replace b_mps_con = _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo]                     	if _n == `h'+1
		replace u_mps_con = _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo] + 1.645* _se[c.`mps'_con#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace d_mps_con = _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo] - 1.645* _se[c.`mps'_con#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace uu_mps_con = _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo] + _se[c.`mps'_con#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1
		replace dd_mps_con = _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo] - _se[c.`mps'_con#c.L.ivol_`matur'_hi2lo]  	if _n == `h'+1	
		
		
		replace b_mpsE = _b[`mps'_exp]                     	if _n == `h'+1
		replace u_mpsE = _b[`mps'_exp] + 1.645* _se[`mps'_exp]  	if _n == `h'+1
		replace d_mpsE = _b[`mps'_exp] - 1.645* _se[`mps'_exp]  	if _n == `h'+1
		replace uu_mpsE = _b[`mps'_exp] + _se[`mps'_exp]  	if _n == `h'+1
		replace dd_mpsE = _b[`mps'_exp] - _se[`mps'_exp]  	if _n == `h'+1	
		
		lincom _b[`mps'_exp] + _b[c.`mps'_exp#c.L.ivol_`matur'_hi2lo ]
		replace b_intEffE = r(estimate)                  if _n == `h'+1
		replace u_intEffE = r(estimate) + 1.645* r(se)  	if _n == `h'+1
		replace d_intEffE = r(estimate) - 1.645* r(se) 	if _n == `h'+1	
		replace uu_intEffE = r(estimate) + r(se)  	if _n == `h'+1
		replace dd_intEffE = r(estimate) - r(se) 	if _n == `h'+1	
		
		replace b_mpsC = -_b[`mps'_con]                     	if _n == `h'+1
		replace u_mpsC = -_b[`mps'_con] + 1.645* _se[`mps'_con]  	if _n == `h'+1
		replace d_mpsC = -_b[`mps'_con] - 1.645* _se[`mps'_con]  	if _n == `h'+1
		replace uu_mpsC = -_b[`mps'_con] + _se[`mps'_con]  	if _n == `h'+1
		replace dd_mpsC = -_b[`mps'_con] - _se[`mps'_con]  	if _n == `h'+1	
		
		lincom _b[`mps'_con] + _b[c.`mps'_con#c.L.ivol_`matur'_hi2lo ]
		replace b_intEffC = -r(estimate)                  if _n == `h'+1
		replace u_intEffC = -r(estimate) + 1.645* r(se)  	if _n == `h'+1
		replace d_intEffC = -r(estimate) - 1.645* r(se) 	if _n == `h'+1	
		replace uu_intEffC = -r(estimate) + r(se)  	if _n == `h'+1
		replace dd_intEffC = -r(estimate) - r(se) 	if _n == `h'+1	
				
		
		replace nobs = e(N)                        						if _n == `h'+1	
		di e(N)
		
		eststo
		
	}

	* Plot the Investment response	
	twoway (rarea d_intEffE u_intEffE qtrs, fc(red%25) lw(none) lpattern(solid)) ///
	(rarea dd_intEffE uu_intEffE qtrs, fc(red%50) lw(none) lpattern(solid)) ///
	(rarea d_mpsE u_mpsE qtrs, fc(blue%25) lw(none) lpattern(solid)) ///
	(rarea dd_mpsE uu_mpsE qtrs, fc(blue%50) lw(none) lpattern(solid)) ///
	(line b_intEffE qtrs, lcolor(red) lpattern(solid) lwidth(thick) xlabel(,labsize(medlarge) nogrid) ylabel(,labsize(medlarge) nogrid)) ///
	(line b_mpsE qtrs, lcolor(blue) lpattern(solid) lwidth(thick)) /// 
	(line zero qtrs, lcolor(black)) if !missing(qtrs), legend(off) ///
	ytitle("Percent", size(medlarge)) xtitle("Quarter", size(medlarge)) ///
	graphregion(color(white)) plotregion(color(white))

	graph export "${output}figureA3_Expansionary.pdf", replace
	
	twoway (rarea d_intEffC u_intEffC qtrs, fc(red%25) lw(none) lpattern(solid)) ///
	(rarea dd_intEffC uu_intEffC qtrs, fc(red%50) lw(none) lpattern(solid)) ///
	(rarea d_mpsC u_mpsC qtrs, fc(blue%25) lw(none) lpattern(solid)) ///
	(rarea dd_mpsC uu_mpsC qtrs, fc(blue%50) lw(none) lpattern(solid)) ///
	(line b_intEffC qtrs, lcolor(red) lpattern(solid) lwidth(thick) xlabel(,labsize(medlarge) nogrid) ylabel(,labsize(medlarge) nogrid)) ///
	(line b_mpsC qtrs, lcolor(blue) lpattern(solid) lwidth(thick)) /// 
	(line zero qtrs, lcolor(black)) if !missing(qtrs), legend(off) ///
	ytitle("Percent", size(medlarge)) xtitle("Quarter", size(medlarge)) ///
	graphregion(color(white)) plotregion(color(white))

	graph export "${output}figureA3_Contractionary.pdf", replace
	
	drop qtrs zero nobs
	
	eststo clear	
}
